package nu.aing.P20140421;

import java.util.Arrays;

/**
 * @author memekmaut@gmail.com
 */
public class TaroFriends {
    
    public static void main(String[] args) {
        TaroFriends obj = new TaroFriends();
        System.out.println(obj.getNumber(
                new int[] {-3, 0, 1}, 
                3)
        );
        System.out.println(obj.getNumber(
                new int[] {4, 7, -7}, 
                5)
        );
        System.out.println(obj.getNumber(
                new int[] {-100000000, 100000000}, 
                100000000)
        );
        System.out.println(obj.getNumber(
                new int[] {3, 7, 4, 6, -10, 7, 10, 9, -5}, 
                7)
        );
        System.out.println(obj.getNumber(
                new int[] {-4, 0, 4, 0}, 
                4)
        );
        System.out.println(obj.getNumber(
                new int[] {7}, 
                0)
        );
    }
    
    public int getNumber(int[] coordinates, int X) {
        Arrays.sort(coordinates);
        final int l = coordinates.length;
        final int M = coordinates[l-1] - ((coordinates[l-1] - coordinates[0]) / 2);
        
        for(int i = 0; i < l; i++) {
            coordinates[i] = (coordinates[i] < M) ? 
                    coordinates[i] + X : coordinates[i] - X;
        }
        Arrays.sort(coordinates);
        
        return (coordinates[l-1] - coordinates[0]);
    }
    
}
